export default {
    onUserStateChange(callback) {
        // 设置一个观察者
        this.auth.onLoginStateChanged(callback);
    },
    isSignIn() {
        const auth = this.auth;
        return auth.hasLoginState() || false;
    },
    SignOut() {
        return this.auth.signOut();
    },
    // 注册
    SignUp({ type = "email", username, password }) {
        if (type === "email") {
            return this.auth
                .signUpWithEmailAndPassword(username, password)
                .catch((err) => err);
        }
    },
    // 登录
    SignIn({ type = "email", username, password }) {
        // 应用初始化时
        if (this.auth.hasLoginState()) return null;
        switch (type) {
            case "email":
                return this.auth.signInWithEmailAndPassword(username, password);
            case "user":
                return this.auth.signInWithUsernameAndPassword(
                    username,
                    password
                );
        }
    },
    async Reset(username) {
        if (/@/.test(username)) {
            return this.auth.sendPasswordResetEmail(username);
        }
    },
};
